import type { ApplianceSettingDes, SettingGroupDataType } from '@/api/appliance'

export type SettingGroupListType = {
  settingsGroup: string
  settingsGroupName: string
  settings: ApplianceSettingDes[]
}
export type NewApplianceSettingDes = ApplianceSettingDes & {
  checked?: boolean
}

export function getSettingGroupData(settings: ApplianceSettingDes[], settingsGroupList: SettingGroupDataType) {
  let settingGroupData: SettingGroupListType[] = [
    {
      settingsGroup: 'SettingsGroup.TopSettings',
      settingsGroupName: '置顶设置',
      settings: [],
    },
    {
      settingsGroup: 'SettingsGroup.TimeSettings',
      settingsGroupName: '时间设置',
      settings: [],
    },
    {
      settingsGroup: 'SettingsGroup.SoundSettings',
      settingsGroupName: '提示音设置',
      settings: [],
    },
    {
      settingsGroup: 'SettingsGroup.DisplaySettings',
      settingsGroupName: '显示设置',
      settings: [],
    },
    {
      settingsGroup: 'SettingsGroup.OtherSettings',
      settingsGroupName: '其他设置',
      settings: [],
    },
  ]

  settings.forEach((setting) => {
    let flag = false
    if (typeof setting.value === 'boolean') {
      setting.checked = setting.value
    }

    settingsGroupList.forEach((group) => {
      group.settings.forEach((groupSetting) => {
        if (setting.key === groupSetting.key) {
          flag = true
          if (group.settingsGroup === 'SettingsGroup.TopSettings') {
            settingGroupData[0].settings.push(setting)
          } else if (group.settingsGroup === 'SettingsGroup.TimeSettings') {
            settingGroupData[1].settings.push(setting)
          } else if (group.settingsGroup === 'SettingsGroup.SoundSettings') {
            settingGroupData[2].settings.push(setting)
          } else if (group.settingsGroup === 'SettingsGroup.DisplaySettings') {
            settingGroupData[3].settings.push(setting)
          }
        }
      })
    })

    if (!flag && !judgeIsFavoriteSetting(setting) && !judgeIsPresentSetting(setting)) {
      settingGroupData[settingGroupData.length - 1].settings.push(setting)
    }
  })

  let list = settingGroupData.filter((item) => item.settings.length > 0)
  return list
}

/**
 * 判断是否是收藏setting
 * @param setting
 * @returns
 */
export function judgeIsFavoriteSetting(setting: ApplianceSettingDes) {
  return setting.key.indexOf('BSH.Common.Setting.Favorite') !== -1
}
export function judgeIsPresentSetting(setting: ApplianceSettingDes) {
  return setting.key.indexOf('Refrigeration.Common.Setting.Preset') !== -1
}
